﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 剧情设计器2._0
{
    public partial class ActionEditor : Form
    {
        Action action;
        public ActionEditor(Action a)
        {
            action = a;
            InitializeComponent();
        }

        void UpdateDisplay()
        {
            comboBox_emitter.Text = action.emitter;
            comboBox_receiver.Text = action.receiver;
            edit_detail.Text = action.description;

            comboBox_emitter.BeginUpdate();
            comboBox_receiver.BeginUpdate();
            comboBox_emitter.Items.Clear();
            comboBox_receiver.Items.Clear();
            foreach(Character c in Common.characters)
            {
                comboBox_emitter.Items.Add(c.name);
                comboBox_receiver.Items.Add(c.name);
            }
            comboBox_emitter.EndUpdate();
            comboBox_receiver.EndUpdate();

            comboBox_actiontype.BeginUpdate();
            comboBox_actiontype.Items.Clear();
            foreach(string type in Common.action_types)
            {
                comboBox_actiontype.Items.Add(type);
            }
            comboBox_actiontype.Text = action.type;
            comboBox_actiontype.EndUpdate();
        }

        private void ActionEditor_Load(object sender, EventArgs e)
        {
            UpdateDisplay();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            action.emitter = comboBox_emitter.Text;
            action.receiver = comboBox_receiver.Text;
            action.description = edit_detail.Text;
            action.type = comboBox_actiontype.Text;

            if (!Common.inList(comboBox_actiontype.Text, Common.action_types) && comboBox_actiontype.Text != "")
            {
                if (MessageBox.Show(string.Format("发现一个未定义的动作类型：{0}\n是否将其添加到动作类型预设中?", comboBox_actiontype.Text), "发现新动作", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Common.action_types.Add(comboBox_actiontype.Text);
                }
            }

            this.Close();
        }

        private void ActionEditor_FormClosed(object sender, FormClosedEventArgs e)
        {
            Program.mainform.UpdateDisplay();
        }

        private void link_editRelationship_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RelationshipsEditor rse = new RelationshipsEditor(action.relationships);
            rse.Show();
        }
    }
}
